
/* Generated by Interface Builder */

#import "LPCreate.h"
#import "Dispatcher.h"
#import <appkit/Application.h>
#import <appkit/Window.h>
#import <stdio.h>
#import <appkit/View.h>
#import <appkit/Cell.h>
#import <appkit/OpenPanel.h>
#import <strings.h>
#import <sound/sound.h>
#import <appkit/ScrollView.h>
#import <appkit/Text.h>
#import <streams/streams.h>
#import <libc.h>
#import <sys/wait.h>

@implementation LPCreate

+ new
{
	self = [super new];
	[NXApp loadNibSection:"LPCreate.nib" owner:self];
	[LPCpoles setIntValue:[NXApp poles]];
	[Plowcps setFloatValue:[NXApp lowcps]];
	[Phighcps setFloatValue:[NXApp highcps]];
	[Pframe setIntValue:[NXApp ptchframe]];
	[LPCframes setIntValue:[NXApp frames]];
	[Pinter setIntValue:[NXApp frames]];
	[self show:self];
	return self;
}

- getLPCFile:sender
{
	id	openP;
 	const char *types[2];
	
	types[0] = "lpc";
	types[1] = 0;
	openP = [OpenPanel new];
	if ([openP runModalForTypes:types]) {
		strcpy(lpcFilename, [openP filename]);
		[LPCfield setStringValue:lpcFilename];
	}
	return self;
}

- go:sender
{
	int	frames, poles, frameS, id1, id2, status, fd;
	float	inskip, dur, lowcps, highcps;
	char	cmd[256], temp[36], type[2];
	FILE	*stream;
	NXStream	*diags;
	
	frames = [LPCframes intValue];
	if (!frames) 
		frames = 250;
	[Pinter setIntValue:frames];
	poles = [LPCpoles intValue];
	if (!poles)
		poles = [NXApp poles];
	inskip = [LPCinskip floatValue];
	if (!strcmp([LPCdur stringValue], "all")) {
		dur = -1;
	}
	else {
		dur = [LPCdur floatValue];
	}
	strcpy(lpcFilename, [LPCfield stringValue]);
	if (!strlen(lpcFilename)) {
		[self badFile:"LPC"];
		return self;
	}
	strcpy(soundFilename, [Soundfield stringValue]);
	if (!strlen(soundFilename)) {
		[self badFile:"Sound"];
		return self;
	}
	strcpy(pchFilename, [Pitchfield stringValue]);
	if (!strlen(pchFilename)) {
		[self badFile:"Pitch analysis"];
		return self;
	}
	sprintf(cmd, "%s -o %s -p %d -f %d ", [NXApp lpc], lpcFilename, poles, frames);
	if (inskip > 0) {
		sprintf(temp, "-i %f ", inskip);
		strcat(cmd, temp);
	}
	if (dur > 0) {
		sprintf(temp, "-d %f ", dur);
		strcat(cmd, temp);
	}		
	strcat(cmd, soundFilename);
	printf("calling lpc....\n");
	printf("cmd: %s\n", cmd);
	system(cmd);
	sprintf(cmd, "%s %s %d", [NXApp stabilize], lpcFilename, poles);	
	printf("calling stablilize.....\n");	
	printf("cmd: %s\n", cmd);
	system(cmd);
	frameS = [Pframe intValue];
	if (!frameS)
		frameS = 350;
	lowcps = [Plowcps floatValue];
	if (!lowcps)
		lowcps = 100.0;
	highcps = [Phighcps floatValue];
	if (!highcps) 
		highcps = 1000.0;
	inskip = [Pinskip floatValue];
	if (!strcmp([Pdur stringValue], "all")) {
		dur = 99;
	}
	else {
		dur = [LPCdur floatValue];
	}
	strcpy(pchFilename, [Pitchfield stringValue]);
	type[0] = 'w';
	type[1] = '\0';
	printf("calling ptrack.....\n");

	if (!(id1 = fork())) {
		fd = open("/tmp/output", O_CREAT|O_RDWR, 0644);
		dup2(fd, fileno(stderr));
		sprintf(cmd, "%s -q ", [NXApp ptrack]);
		stream = popen(cmd, type);
		fprintf(stream, "%s\nn\n%s\n%d\n%d\n%f\n%f\n%f\n%f\n", soundFilename, pchFilename, frameS, frames, lowcps, highcps, inskip, dur);
		fclose(stream);
		while ((wait((union wait *)&status) != -1)) 
			;
		close(fd);
		exit(1);
	}
	while ((wait((union wait *)&status) != -1))
		;
	diags = NXMapFile("/tmp/output", NX_READONLY);
	[[diagScroll docView] readText:diags];
	NXCloseMemory(diags, NX_FREEBUFFER);
	if (!(id2 = fork())) {
		printf("Calling merge : %s\n", [NXApp merge]);
		fd = open("/tmp/output", O_CREAT|O_WRONLY|O_TRUNC, 0644);
		dup2(fd, fileno(stdout));
		stream = popen([NXApp merge], type);
		fprintf(stream, "%s\n%s\n%d\n1\n1\n999\n", lpcFilename, pchFilename, poles);
		fclose(stream);
		while ((wait((union wait *)&status) != -1))
			;
		close(fd);
		exit(1);
	}
	while ((wait((union wait *)&status) != -1))
		;
	diags = NXMapFile("/tmp/output", NX_READONLY);
	[[diagScroll docView] selectAll:self];
	[[diagScroll docView] cut:self];
	[[diagScroll docView] readText:diags];
	[[diagScroll docView] setSel:0:0];
	[[diagScroll docView] paste:self];
	NXCloseMemory(diags, NX_FREEBUFFER);
	printf("done!\n");
	return self;
}

- doLPC:sender
{
	int	frames, poles;
	float	inskip, dur;
	char	cmd[256], temp[36];

	frames = [LPCframes intValue];
	if (!frames) 
		frames = 250;
	[Pinter setIntValue:frames];
	poles = [LPCpoles intValue];
	if (!poles)
		poles = 24;
	inskip = [LPCinskip floatValue];
	if (!strcmp([LPCdur stringValue], "all")) {
		dur = -1;
	}
	else {
		dur = [LPCdur floatValue];
	}
	strcpy(lpcFilename, [LPCfield stringValue]);
	if (!strlen(lpcFilename)) {
		[self badFile:"LPC"];
		return self;
	}
	strcpy(soundFilename, [Soundfield stringValue]);
	if (!strlen(soundFilename)) {
		[self badFile:"Sound"];
		return self;
	}
	strcpy(pchFilename, [Pitchfield stringValue]);
	if (!strlen(pchFilename)) {
		[self badFile:"Pitch Analysis"];
		return self;
	}
	sprintf(cmd, "%s -o %s -p %d -f %d ", [NXApp lpc], lpcFilename, poles, frames);
	if (inskip > 0) {
		sprintf(temp, "-i %f ", inskip);
		strcat(cmd, temp);
	}
	if (dur > 0) {
		sprintf(temp, "-d %f ", dur);
		strcat(cmd, temp);
	}		
	strcat(cmd, soundFilename);
	printf("calling lpc....\n");
	printf("cmd: %s\n", cmd);
	system(cmd);
	sprintf(cmd, "%s %s %d", [NXApp stabilize], lpcFilename, poles);	
	printf("calling stablilize.....\n");	
	printf("cmd: %s\n", cmd);
	system(cmd);
	return self;
}

- doPtrack:sender
{
	int	frames, frameS, id1, status;
	float	inskip, dur, lowcps, highcps;
	char	type[2], cmd[256];
	FILE	*stream;
	
	frames = [LPCframes intValue];
	frameS = [Pframe intValue];
	if (!frameS)
		frameS = 350;
	lowcps = [Plowcps floatValue];
	if (!lowcps)
		lowcps = 100.0;
	highcps = [Phighcps floatValue];
	if (!highcps) 
		highcps = 1000.0;
	inskip = [Pinskip floatValue];
	if (!strcmp([Pdur stringValue], "all")) {
		dur = 99;
	}
	else {
		dur = [LPCdur floatValue];
	}
	strcpy(soundFilename, [Soundfield stringValue]);
	if (!strlen(soundFilename)) {
		[self badFile:"Sound"];
		return self;
	}
	strcpy(pchFilename, [Pitchfield stringValue]);
	if (!strlen(pchFilename)) {
		[self badFile:pchFilename];
		return self;
	}
	type[0] = 'w';
	type[1] = '\0';
	printf("calling ptrack.....\n");

	if (!(id1 = fork())) {
		sprintf(cmd, "%s -q ", [NXApp ptrack]);
		stream = popen("/Net/dobro/musr/bin/ptrack -q ", type);
		fprintf(stream, "%s\nn\n%s\n%d\n%d\n%f\n%f\n%f\n%f\n", soundFilename, pchFilename, frameS, frames, lowcps, highcps, inskip, dur);
		fclose(stream);
		while ((wait((union wait *)&status) != -1)) 
			;
		exit(1);
	}
	while ((wait((union wait *)&status) != -1))
		;

	return self;
}

- doMerge:sender
{
	int	id1, status, poles;
	char	type[2];
	FILE	*stream;
	NXStream	*output;
	
	strcpy(lpcFilename, [LPCfield stringValue]);
	if (!strlen(lpcFilename)) {
		[self badFile:"LPC"];
		return self;
	}
	strcpy(pchFilename, [Pitchfield stringValue]);
	if (!strlen(pchFilename)) {
		[self badFile:"Pitch Analysis"];
		return self;
	}
	poles = [LPCpoles intValue];
	if (!poles) {
		poles = [NXApp poles];
	}
	type[0] = 'w';
	type[1] = '\0';
	if (!(id1 = fork())) {
		output = NXOpenFile(id1, NX_READWRITE);
		stream = popen([NXApp merge], type);
		fprintf(stream, "%s\n%s\n%d\n1\n1\n999\n", lpcFilename, pchFilename, poles);
		fclose(stream);
		NXClose(output);
		while ((wait((union wait *)&status) != -1))
			;
		exit(1);
	}

	while ((wait((union wait *)&status) != -1))
		;
	
	return self;
}

- viewLPC:sender
{
	strcpy(lpcFilename, [LPCfield stringValue]);
	if (!strlen(lpcFilename))
		return self;
	[NXApp viewFile:lpcFilename];
	return self;
}

- viewPCH:sender
{
	strcpy(pchFilename, [Pitchfield stringValue]);
	if (!strlen(pchFilename))
		return self;
	[NXApp viewPCHfile:pchFilename];
	return self;
}

- playSound:sender
{
	int	err;
	SNDSoundStruct *s;
	
	if (!strlen(soundFilename))
		return self;
	err = SNDReadSoundfile(soundFilename, &s);
	if (!err) {
		err = SNDStartPlaying(s, 1, 5, 0, 0,(SNDNotificationFun)SNDFree);
		if (!err)
			SNDWait(1);
	}
	return self;
}

- doHZ:sender
{
	float	val;
	char	cmd[256];
	
	val = [HZfield floatValue];
	sprintf(cmd, "%s %f\n", [NXApp hz], val);
	system(cmd);
	return self;
}

- getPitchFile:sender
{
	id	openP;
 	const char *types[2];
	
	types[0] = "pch";
	types[1] = 0;
	openP = [OpenPanel new];
	if ([openP runModalForTypes:types]) {
		strcpy(pchFilename, [openP filename]);
		[Pitchfield setStringValue:pchFilename];
	}
	return self;
}

- getSoundFile:sender
{
	id	openP;
	const char *types[2];
	char	*t, *s;
	
	types[0] = "snd";
	types[1] = 0;
	
	openP = [OpenPanel new];
	if ([openP runModalForTypes:types]) {
		strcpy(soundFilename, [openP filename]);
		[Soundfield setStringValue:soundFilename];
		t = rindex(soundFilename, '/');
		s = rindex(t, '.');
		if (!strlen(lpcFilename)) {
			strncpy(lpcFilename, soundFilename, (int)(s - soundFilename));
			strcat(lpcFilename, ".lpc");
			[LPCfield setStringValue:lpcFilename];
		}

		if (!strlen(pchFilename)) {
			strncpy(pchFilename, soundFilename, (int)(s - soundFilename));
			strcat(pchFilename, ".pch");
			[Pitchfield setStringValue:pchFilename];
		}
	}
	return self;
}

- show:sender
{
	[myWin makeKeyAndOrderFront:self];
	return self;
}

- badFile:(char *)type
{
	NXRunAlertPanel("Alert", "Invalid %s filename\n", "Ok", NULL, NULL, type);
	return self;
}






@end
